/**
 * 测试人员许可证路由
 *
 * Created by snowingsea on 2020/02/24.
 */
const middleware = require('./../middleware');
const licenseModel = require('./../../models/license');
const { fixLicenseStatusResponse } = require('../../helpers/licenseHelper');
const PhoenixError = require('../../models/phoenix-error');

/**
 * 获取许可证信息
 * @param req
 * @param res
 * @param next
 */
function getLicenseStatus(req, res, next) { // eslint-disable-line no-unused-vars
  (async () => {
    const license = await fixLicenseStatusResponse(licenseModel.getLicenseStatusJson(req.$license));
    res.json(license);
  })().catch((err) => {
    next(new PhoenixError(err));
  });
}

/**
 * Export model definition object.
 */
module.exports = (app, prefix) => {
  const licenseStatusURL = `${prefix}/license/status.json`;

  // 获取许可证状态
  app.get(licenseStatusURL, middleware.user.requireTokenUnexpired);
  app.get(licenseStatusURL, middleware.user.requireAvailable);
  app.get(licenseStatusURL, middleware.user.requireTester);
  app.get(licenseStatusURL, middleware.license.requireLicenseExist);
  app.get(licenseStatusURL, getLicenseStatus);
};
